package com.jin10.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jin10.bean.BaseResponse;
import com.jin10.entity.BaseInfo;
import com.jin10.service.IBaseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  基本数据信息差
 * </p>
 *
 * @author raofy
 * @since 2022-03-09
 */
@RestController
@RequestMapping("/baseInfo")
public class BaseInfoController {

    @Autowired
    IBaseInfoService iBaseInfoService;

    @GetMapping("getNonFarmBaseInfo")
    public BaseResponse getNonFarmBaseInfo() {
        List<BaseInfo> list = iBaseInfoService.list(new LambdaQueryWrapper<BaseInfo>().orderByDesc(BaseInfo::getId));
        if (list == null && list.isEmpty()) {
            return BaseResponse.ok(new BaseInfo());
        } else {
            return BaseResponse.ok(list.get(0));
        }
    }

    @PostMapping("saveOrUpdate")
    public BaseResponse saveOrUpdate(@RequestBody BaseInfo dto) {
        if (Objects.isNull(dto.getId())) {
            return BaseResponse.ok(iBaseInfoService.save(dto));
        } else {
            return BaseResponse.ok(iBaseInfoService.updateById(dto));
        }
    }

}
